package Randomwriting;
import java.util.Vector;
import structure5.*;

public class FreqList {
	public Vector<Association> charCount = new Vector<Association>();//field property
	
	public FreqList(String c)
	{
		charCount.add(new Association(c,new Integer(1)));
	}
	
	public void updateCount(String c)
	{
		int i;
		for (i = 0; i < charCount.size(); i++)
		{
			Association charInfo;
			String existingChar;
			
			// get the association
			charInfo = (Association)charCount.get(i);
			// get the word from the association
			existingChar = (String)charInfo.getKey();
			
			if (existingChar.equals(c))
			{ 
				// match: increment integer in association
				Integer f = (Integer)charInfo.getValue();
				charInfo.setValue(new Integer(f.intValue() + 1));
				break;
			}
		}
		// mismatch: add new word, frequency 1.
		if (i == charCount.size())
		{
			charCount.add(new Association(c,new Integer(1)));
		}
	}
	
	public String getHighestFreqChar()
	{
		String highestFreqChar="";
		int Highestfreq=0;
		Integer f;
		
		Association charInfo;
		for(int i=0; i<charCount.size(); i++)
		{
			charInfo = (Association)charCount.get(i);
			f = (Integer)charInfo.getValue();
			if(f>Highestfreq)
			{
				highestFreqChar=(String)charInfo.getKey();
				Highestfreq=f;
			}
		}
		return highestFreqChar;
	}
	
	public void printFrequencies()
	{
		Association charInfo;
		for(int i=0; i<charCount.size(); i++)
		{
			charInfo= (Association)charCount.get(i);
			System.out.println(
			charInfo.getKey()+" occurs "+
			charInfo.getValue()+" times.");
		}
	}
	
}
